<template>
	<view class="index">
		<view class="form">
			<u-form :model="form" ref="uForm" class="u-form">
				<u-form-item prop="username" left-icon="account">
					<u-input placeholder="请输入用户名" v-model="form.username"></u-input>
				</u-form-item>
				<u-form-item prop="password" left-icon="lock">
					<u-input type="password" placeholder="请输入密码" v-model="form.password"></u-input>
				</u-form-item>
				<u-form-item prop="mobile" left-icon="phone">
					<u-input placeholder="请输入手机号" v-model="form.mobile"></u-input>
				</u-form-item>
				<u-form-item prop="captcha" left-icon="checkbox-mark">
					<u-input placeholder="请输入验证码" v-model="form.captcha"></u-input>
					<view class="right" slot="right">
						<u-image 
							:src="captcha.image"
							height="70"
							width="200"
							border-radius="20"
							@click="loadCaptcha"
						></u-image>
					</view>
				</u-form-item>
			</u-form>
			<u-button 
				type="primary"
				class="submit-button"
				@click="submit"
			>注册</u-button>
		</view>
		<u-toast ref="Toast"></u-toast>
	</view>
</template>

<script>
	import {mapMutations} from 'vuex'
	export default {
		data() {
			return {
				form: {
					username: '',
					password: '',
					mobile: '',
					captcha: '',
				},
				captcha: {
					image: '',
					captcha_key: ''
				},
				formRule: {
					username: [
						{
							required: true,
							message: '请输入用户名',
							trigger: ['change','blur']
						},
						{
							min:3,
							message: '用户名不能少于3位',
							trigger: ['change','blur']
						},
						{
							asyncValidator: (rule,value,callback) => {
								return this.$api.user.checkUsername({
									username: this.form.username
								})
								.then(res=>{
									if(res.code == 0)
										callback(new Error(res.msg))
									else
										callback()
								})
							},
							trigger: 'blur'
						}
					],
					password: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change','blur']
						},
						{
							min:3,
							message: '密码不能少于3为',
							trigger: ['change','blur']
						}
					],
					mobile: [
						{
							required: true,
							message: '请输入手机号',
							trigger: ['change','blur']
						},
						{
							validator: (rule,value,callback)=>{
								return this.$u.test.mobile(value)
							},
							message: '手机号格式不正确',
							trigger: ['blur']
						},
						{
							asyncValidator: (rule,value,callback)=>{
								return this.$api.user.checkMobile({
									mobile: this.form.mobile
								})
								.then(res=>{
									if(res.code == 0)
										callback(new Error(res.msg))
									else
										callback()
								})
							},
							trigger: 'blur'
						}
					],
					captcha: [
						{
							required: true,
							message: '请输入验证码',
							trigger: ['change','blur']
						},
						{
							len: 4,
							message: '验证码必须是4位字符',
							trigger: 'blur'
						}
					]
				},
			};
		},
		methods: {
			...mapMutations(['saveUserinfo']),
			loadCaptcha(){
				return this.$api.other.captcha()
				.then(res=>{
					if(res.code == 1)
						this.captcha = res.data
				})
			},
			submit(){
				this.$refs.uForm.validate(isValidate=>{
					if(isValidate){
						this.register()
					}else{
						this.$refs.Toast.show({
							title: '您填入的信息有误' ,
							type: 'warning'
						})
					}
				})
			},
			register(){
				return this.$api.user.register({
					captcha_key: this.captcha.captcha_key,
					...this.form
				})
				.then(res=>{
					if(res.code == 1){
						// 自动登录 并跳到用户中心
						this.saveUserinfo(res.data.userinfo)
						this.$refs.Toast.show({
							title: '注册成功',
							type: 'success',
							callback: ()=>{
								this.$Router.pushTab({name: 'user.index'})
							}
						})
					}else{
						this.$refs.Toast.show({
							title: res.msg,
							type: 'warning'
						})
					}
				})
			}
		},
		mounted(){
			this.loadCaptcha()
			this.$refs.uForm.setRules(this.formRule)
		}
	}
</script>

<style lang="scss" scoped>
	.index{
		background-color: $uni-bg-color;
		.form{
			padding: 0 $uni-spacing-row-lg;
			.u-form{
				margin-bottom: 62upx;
			}
			.submit-button{
				width: 654upx;
				height: 90upx;
				border-radius: 90upx;
				color: #333333;
				font-weight: bold;
			}
		}
	}
</style>
